﻿using System;
using System.Collections.Generic;
using System.Text;
using PreeRegisterModel;
using System.Data;
using PreeRegisterDAL;
using DrSoftsFrame;
using Newtonsoft.Json.Linq;
using System.Web;

namespace PreeRegisterBLL
{
    public class SearchBLL_2
    {
        #region 长治和平接口 查询缴费项 订单支付

        /// <summary>
        /// 长治和平 查询缴费项
        /// </summary>
        /// <param name="notPayRequestModel"></param>
        /// <returns></returns>
        public static NotPayResponseModel NotPay_CZHP(NotPayRequestModel notPayRequestModel)
        {
            try
            {
                //查询缴费项
                NotPayResponseModel notPayResponseModel = new PreeRegisterModel.NotPayResponseModel();

                //获取查询缴费项
                List<NotPayResponseItemModel> NotPayResponseItemModel_list = new List<NotPayResponseItemModel>();

                try
                {
                    string url_searchNopay = CommonBLL.GetConfig("CZHP_ADDR") + "GetAppointmentByOrderId?parameters=";
                    StringBuilder head = new StringBuilder();
                    StringBuilder body = new StringBuilder();

                    head.Append("{head:\"\"");
                    body.Append(" {");
                    body.AppendFormat("\"OrderId\": \"{0}\"", CommonBLL.GetOrderId(notPayRequestModel.orderid, 3));
                    body.Append("}");
                    CommonBLL.WriteFile_New(CommonBLL.bizCode_searchNopay, "1：查询缴费项接口请求body明文：\r\n" + CommonBLL.ConvertJsonString(body.ToString()));
                    string postData_searchNopay = string.Format("{0},body:\"{1}", head.ToString(), HttpUtility.UrlEncode(Security.Base64.EncodeBase64(body.ToString())));
                    postData_searchNopay += "\"}";
                    url_searchNopay += postData_searchNopay;
                    CommonBLL.WriteFile_New(CommonBLL.bizCode_searchNopay, "2：查询缴费项接口请求密文：\r\n" + url_searchNopay);
                    JObject obj_searchNopay = CommonBLL.HttpPost_CZHP(url_searchNopay);
                    CommonBLL.WriteFile_New(CommonBLL.bizCode_searchNopay, "3：查询缴费项接口返回明文：\r\n" + CommonBLL.ConvertJsonString(obj_searchNopay.ToString()));
                    string code_searchNopay = obj_searchNopay["code"].ToString();

                    if (string.Equals(code_searchNopay, "1"))
                    {
                        string code = "";
                        string msg = "";
                        var mJObj = JArray.Parse(obj_searchNopay["result"].ToString());
                        foreach (var drNotPay in mJObj)
                        {
                            NotPayResponseItemModel notPayResponseItemModel = new NotPayResponseItemModel();
                            //0 Available 2 Charged 3 Triaged  4 Appiontmented
                            if (string.Equals(drNotPay["AppointmentOrder"]["Status"].ToString(), "Available")//0
                                || string.Equals(drNotPay["AppointmentOrder"]["Status"].ToString(), "Charged")//2
                                || string.Equals(drNotPay["AppointmentOrder"]["Status"].ToString(), "Triaged")//3
                                 || string.Equals(drNotPay["AppointmentOrder"]["Status"].ToString(), "Appiontmented"))//4
                            {
                                code = "1";
                                msg = "查询缴费项成功";
                                notPayResponseItemModel.name = drNotPay["Patient"]["PatientName"].ToString();
                                notPayResponseItemModel.gender = drNotPay["Patient"]["Gender"].ToString();
                                notPayResponseItemModel.state = "1";
                                notPayResponseItemModel.dname = drNotPay["WorkSchedule"]["Department"]["Name"].ToString();
                                notPayResponseItemModel.docname = drNotPay["WorkSchedule"]["Doctor"]["Name"].ToString();
                                notPayResponseItemModel.time = drNotPay["AppointmentOrder"]["OrderDate"].ToString();
                                notPayResponseItemModel.cateid = drNotPay["AppointmentType"].ToString() == "预约" ? "1" : "";
                                notPayResponseItemModel.title = notPayResponseItemModel.cateid == "1" ? "挂号费" : "诊间支付";
                                notPayResponseItemModel.price = drNotPay["AppointmentOrder"]["Amount"].ToString();

                                notPayResponseItemModel.orderid = notPayRequestModel.orderid;//drNotPay["AppointmentOrder"]["OrderId"].ToString();
                                notPayResponseItemModel.hospitalcode = "1";
                                NotPayResponseItemModel_list.Add(notPayResponseItemModel);
                            }
                            else
                            {
                                code = "0";
                                msg = "没有要缴费的信息。";
                            }
                        }
                        notPayResponseModel.items = NotPayResponseItemModel_list;
                        notPayResponseModel.code = code;
                        notPayResponseModel.msg = msg;

                    }
                }
                catch (Exception err)
                {
                    throw err;
                }
                //返回
                CommonBLL.WriteFile_New(CommonBLL.bizCode_searchNopay, notPayResponseModel.msg);
                return notPayResponseModel;
            }
            catch (Exception err)
            {
                throw;
            }
        }

        /// <summary>
        /// 长治和平 查询缴费项【新】
        /// </summary>
        /// <param name="notPayRequestModel"></param>
        /// <returns></returns>
        public static NotPayResponseModel NotPay_CZHP_New(NotPayRequestModel notPayRequestModel)
        {
            try
            {
                //查询缴费项
                NotPayResponseModel notPayResponseModel = new PreeRegisterModel.NotPayResponseModel();

                //获取查询缴费项
                List<NotPayResponseItemModel> NotPayResponseItemModel_list = new List<NotPayResponseItemModel>();

                try
                {
                    string code = "";
                    string msg = "";
                    PreeRegisterBLL.hars_appointmentrecord bll = new PreeRegisterBLL.hars_appointmentrecord();
                    NotPayResponseItemModel notPayResponseItemModel = new NotPayResponseItemModel();
                    if (!string.IsNullOrEmpty(notPayRequestModel.orderid))
                    {
                        DataSet ds = bll.GetList(string.Format(" isdelete=0 and orderid='{0}'", notPayRequestModel.orderid));
                        if (ds != null && ds.Tables[0].Rows.Count > 0)
                        {
                            code = "1";
                            msg = "查询缴费项成功";
                            notPayResponseItemModel.name = ds.Tables[0].Rows[0]["name"].ToString();
                            notPayResponseItemModel.gender = ds.Tables[0].Rows[0]["gender"].ToString();
                            notPayResponseItemModel.state = "1";
                            notPayResponseItemModel.dname = ds.Tables[0].Rows[0]["deptname"].ToString();
                            notPayResponseItemModel.docname = ds.Tables[0].Rows[0]["doctorname"].ToString();
                            notPayResponseItemModel.time = ds.Tables[0].Rows[0]["tdate"].ToString();
                            notPayResponseItemModel.cateid = "1";
                            notPayResponseItemModel.title = notPayResponseItemModel.cateid == "1" ? "挂号费" : "诊间支付";
                            double ofee = double.Parse(ds.Tables[0].Rows[0]["ofee"].ToString()) / 100;
                            double price = double.Parse(ds.Tables[0].Rows[0]["price"].ToString()) / 100;
                            double price2 = double.Parse(ds.Tables[0].Rows[0]["price2"].ToString()) / 100;
                            double amount = ofee + price + price2;
                            notPayResponseItemModel.price = amount.ToString();//

                            notPayResponseItemModel.orderid = notPayRequestModel.orderid;
                            notPayResponseItemModel.hospitalcode = "1";
                            NotPayResponseItemModel_list.Add(notPayResponseItemModel);

                            notPayResponseModel.items = NotPayResponseItemModel_list;
                            notPayResponseModel.code = code;
                            notPayResponseModel.msg = msg;
                        }
                        else
                        {
                            code = "0";
                            msg = "查询缴费项接口，未查询到预约信息，错误代码：001";
                        }
                    }
                    else
                    {
                        code = "0";
                        msg = "查询缴费项接口，orderid 不能为空，错误代码：002";
                    }

                }
                catch (Exception err)
                {
                    throw err;
                }
                //返回
                CommonBLL.WriteFile_New(CommonBLL.bizCode_searchNopay, notPayResponseModel.msg);
                return notPayResponseModel;
            }
            catch (Exception err)
            {
                throw;
            }
        }

        /// <summary>
        /// 长治和平 支付接口
        /// </summary>
        /// <param name="payResultRequestModel"></param>
        /// <returns></returns>
        public static PayResultResponseItemModel PayResult_CZHP(PayResultRequstModel payResultRequestModel)
        {
            try
            {
                PayResultResponseItemModel payResultResponseItemModel = new PayResultResponseItemModel();
                try
                {
                    string url_payResult = CommonBLL.GetConfig("CZHP_ADDR") + "GetAppointmentByOrderId?parameters=";
                    StringBuilder head = new StringBuilder();
                    StringBuilder body = new StringBuilder();

                    head.Append("{head:\"\"");
                    body.Append(" {");
                    body.AppendFormat("\"OrderId\": \"{0}\"", CommonBLL.GetOrderId(payResultRequestModel.orderid, 3));
                    body.Append("}");
                    CommonBLL.WriteFile_New(CommonBLL.bizCode_payResult, "1：订单支付接口请求body明文：\r\n" + CommonBLL.ConvertJsonString(body.ToString()));
                    string postData_payResult = string.Format("{0},body:\"{1}", head.ToString(), HttpUtility.UrlEncode(Security.Base64.EncodeBase64(body.ToString())));
                    postData_payResult += "\"}";
                    url_payResult += postData_payResult;
                    JObject obj_payResult = CommonBLL.HttpPost_CZHP(url_payResult);
                    CommonBLL.WriteFile_New(CommonBLL.bizCode_payResult, "2：订单支付接口请求密文：\r\n" + url_payResult);
                    string code_payResult = obj_payResult["code"].ToString();
                    CommonBLL.WriteFile_New(CommonBLL.bizCode_payResult, "3：订单支付接口返回明文：\r\n" + CommonBLL.ConvertJsonString(obj_payResult.ToString()));

                    if (string.Equals(code_payResult, "1"))
                    {
                        var mJObj = JArray.Parse(obj_payResult["result"].ToString());
                        foreach (var drNotPay in mJObj)
                        {
                            //0 Available 2 Charged 3 Triaged  4 Appiontmented
                            if (string.Equals(drNotPay["AppointmentOrder"]["Status"].ToString(), "Available")//0
                                || string.Equals(drNotPay["AppointmentOrder"]["Status"].ToString(), "Charged")//2
                                || string.Equals(drNotPay["AppointmentOrder"]["Status"].ToString(), "Triaged"))//3
                            {
                                payResultResponseItemModel.code = "1";
                                payResultResponseItemModel.msg = "支付成功";
                                payResultResponseItemModel.cdiagnosetypename = drNotPay["WorkSchedule"]["Doctor"]["Rank"].ToString();
                                payResultResponseItemModel.cflowcode = CommonBLL.GetOrderId(drNotPay["AppointmentOrder"]["OrderId"].ToString(), 4);
                                payResultResponseItemModel.chousename = drNotPay["WorkSchedule"]["VisitRoom"].ToString();
                                payResultResponseItemModel.chousesectionname = drNotPay["WorkSchedule"]["VisitRoom"].ToString();
                                payResultResponseItemModel.cidentitycard = drNotPay["Patient"]["IdentityCard"].ToString();
                                payResultResponseItemModel.clocation = drNotPay["WorkSchedule"]["VisitRoom"].ToString();
                                payResultResponseItemModel.cpatientcode = drNotPay["Patient"]["IdentityCard"].ToString();
                                payResultResponseItemModel.cpatientname = drNotPay["Patient"]["PatientName"].ToString();
                                payResultResponseItemModel.csex = drNotPay["Patient"]["Gender"].ToString();
                                payResultResponseItemModel.ddiagnosetime = drNotPay["WorkSchedule"]["Date"].ToString();

                                payResultResponseItemModel.deptname = drNotPay["WorkSchedule"]["Department"]["Name"].ToString();
                                payResultResponseItemModel.diagnoseid = drNotPay["AppointmentOrder"]["OrderId"].ToString();
                                payResultResponseItemModel.doctorname = drNotPay["WorkSchedule"]["Doctor"]["Name"].ToString();
                                payResultResponseItemModel.ndiagnosenum = drNotPay["WorkDetail"]["AppointmentOrder"].ToString();
                                payResultResponseItemModel.nmoney = drNotPay["AppointmentOrder"]["Amount"].ToString();
                                //   payResultResponseItemModel_list.Add(payResultResponseItemModel);
                            }
                            else
                            {
                                payResultResponseItemModel.code = "1";
                                payResultResponseItemModel.msg = obj_payResult["msg"].ToString();
                                payResultResponseItemModel.cdiagnosetypename = "";
                                payResultResponseItemModel.cflowcode = "";
                                payResultResponseItemModel.chousename = "";
                                payResultResponseItemModel.chousesectionname = "";
                                payResultResponseItemModel.cidentitycard = "";
                                payResultResponseItemModel.clocation = "";
                                payResultResponseItemModel.cpatientcode = "";
                                payResultResponseItemModel.cpatientname = "";
                                payResultResponseItemModel.csex = "";
                                payResultResponseItemModel.ddiagnosetime = "";

                                payResultResponseItemModel.deptname = "";
                                payResultResponseItemModel.diagnoseid = "";
                                payResultResponseItemModel.doctorname = "";
                                payResultResponseItemModel.ndiagnosenum = "";
                                payResultResponseItemModel.nmoney = "";
                                //  payResultResponseItemModel_list.Add(payResultResponseItemModel);
                            }
                        }
                        //payResultResponseModel.items = payResultResponseItemModel_list;
                        //payResultResponseModel.code = code;
                        //payResultResponseModel.msg = msg;

                    }
                }
                catch (Exception err)
                {
                    throw err;
                }

                //返回
                CommonBLL.WriteFile_New(CommonBLL.bizCode_payResult, payResultResponseItemModel.msg);
                return payResultResponseItemModel;
            }
            catch (Exception err)
            {

                throw;
            }
        }

        /// <summary>
        /// 长治和平 支付接口【新】
        /// </summary>
        /// <param name="payResultRequestModel"></param>
        /// <returns></returns>
        public static PayResultResponseItemModel PayResult_CZHP_New(PayResultRequstModel payResultRequestModel)
        {
            try
            {
                PayResultResponseItemModel payResultResponseItemModel = new PayResultResponseItemModel();
                try
                {
                    hars_appointmentrecord bll = new hars_appointmentrecord();
                    NotPayResponseItemModel notPayResponseItemModel = new NotPayResponseItemModel();
                    if (!string.IsNullOrEmpty(payResultRequestModel.orderid))
                    {
                        DataSet ds = bll.GetList(string.Format(" isdelete=0 and orderid='{0}'", payResultRequestModel.orderid));

                        if (ds != null && ds.Tables[0].Rows.Count > 0)
                        {
                            CommonBLL.WriteFile_New(CommonBLL.bizCode_payResult, "2：查询到患者的预约信息：\r\n" + payResultRequestModel.orderid);
                            #region 存储并判断是否已经请求HIS
                            PreeRegisterModel.hars_notifyhistask model = new PreeRegisterModel.hars_notifyhistask();
                            model.createdate = model.lastdate = DateTime.Now;
                            model.orderno = model.request = payResultRequestModel.orderid;
                            model.state = 0;
                            hars_notifyhistask bll_notify = new hars_notifyhistask();
                            DataSet ds_notify = bll_notify.GetList(string.Format("orderno='{0}'", payResultRequestModel.orderid));
                            if ( ds_notify.Tables[0].Rows.Count == 0)
                            {
                                if (!bll_notify.Add(model))
                                {
                                    payResultResponseItemModel.code = "-7";
                                    payResultResponseItemModel.msg = "数据处理出错，请及时排查处理。";
                                    payResultResponseItemModel.oid = "";
                                    payResultResponseItemModel.cdiagnosetypename = "";
                                    payResultResponseItemModel.cflowcode = "";
                                    payResultResponseItemModel.chousename = "";
                                    payResultResponseItemModel.chousesectionname = "";
                                    payResultResponseItemModel.cidentitycard = "";
                                    payResultResponseItemModel.clocation = "";
                                    payResultResponseItemModel.cpatientcode = "";
                                    payResultResponseItemModel.cpatientname = "";
                                    payResultResponseItemModel.csex = "";
                                    payResultResponseItemModel.ddiagnosetime = "";

                                    payResultResponseItemModel.deptname = "";
                                    payResultResponseItemModel.diagnoseid = "";
                                    payResultResponseItemModel.doctorname = "";
                                    payResultResponseItemModel.ndiagnosenum = "";
                                    payResultResponseItemModel.nmoney = "";
                                    return payResultResponseItemModel;
                                }
                            }
                            else if (ds_notify.Tables[0].Rows[0]["state"].ToString() == "1")
                            {
                                var mJObj_getorder = JArray.Parse(ds_notify.Tables[0].Rows[0]["response"].ToString());
                                foreach (var drGetorder in mJObj_getorder)
                                {
                                    string orderid = drGetorder["AppointmentOrder"]["OrderId"].ToString();

                                    JObject obj_payorder = ChangZhiHePingBLL.GetAppointmentByOrderId(orderid);
                                    string code_payorder = obj_payorder["code"].ToString();
                                    if (string.Equals(code_payorder, "1"))
                                    {
                                        var mJObj_payorder = JArray.Parse(obj_payorder["result"].ToString());
                                        foreach (var drPayorder in mJObj_payorder)
                                        {
                                            if (string.Equals(drPayorder["AppointmentOrder"]["Status"].ToString(), "Available")//0
                                                || string.Equals(drPayorder["AppointmentOrder"]["Status"].ToString(), "Charged")//2
                                                || string.Equals(drPayorder["AppointmentOrder"]["Status"].ToString(), "Triaged"))//3
                                            {
                                                payResultResponseItemModel.code = "1";
                                                payResultResponseItemModel.msg = "支付成功";
                                                payResultResponseItemModel.oid = drPayorder["AppointmentOrder"]["OrderId"].ToString();
                                                payResultResponseItemModel.cdiagnosetypename = drPayorder["WorkSchedule"]["Doctor"]["Rank"].ToString();
                                                payResultResponseItemModel.cflowcode = CommonBLL.GetOrderId(drPayorder["AppointmentOrder"]["OrderId"].ToString(), 4);
                                                payResultResponseItemModel.chousename = drPayorder["WorkSchedule"]["VisitRoom"].ToString();
                                                payResultResponseItemModel.chousesectionname = drPayorder["WorkSchedule"]["VisitRoom"].ToString();
                                                payResultResponseItemModel.cidentitycard = drPayorder["Patient"]["IdentityCard"].ToString();
                                                payResultResponseItemModel.clocation = drPayorder["WorkSchedule"]["VisitRoom"].ToString();
                                                payResultResponseItemModel.cpatientcode = drPayorder["Patient"]["IdentityCard"].ToString();
                                                payResultResponseItemModel.cpatientname = drPayorder["Patient"]["PatientName"].ToString();
                                                payResultResponseItemModel.csex = drPayorder["Patient"]["Gender"].ToString();
                                                payResultResponseItemModel.ddiagnosetime = drPayorder["WorkSchedule"]["Date"].ToString();

                                                payResultResponseItemModel.deptname = drPayorder["WorkSchedule"]["Department"]["Name"].ToString();
                                                payResultResponseItemModel.diagnoseid = drPayorder["AppointmentOrder"]["OrderId"].ToString();
                                                payResultResponseItemModel.doctorname = drPayorder["WorkSchedule"]["Doctor"]["Name"].ToString();
                                                payResultResponseItemModel.ndiagnosenum = drPayorder["WorkDetail"]["AppointmentOrder"].ToString();
                                                payResultResponseItemModel.nmoney = drPayorder["AppointmentOrder"]["Amount"].ToString();
                                            }
                                            else
                                            {
                                                payResultResponseItemModel.code = "1";
                                                payResultResponseItemModel.msg = "支付成功，医院侧订单状态：" + drPayorder["AppointmentOrder"]["Status"].ToString();
                                                payResultResponseItemModel.oid = drPayorder["AppointmentOrder"]["OrderId"].ToString();
                                                payResultResponseItemModel.cdiagnosetypename = "";
                                                payResultResponseItemModel.cflowcode = "";
                                                payResultResponseItemModel.chousename = "";
                                                payResultResponseItemModel.chousesectionname = "";
                                                payResultResponseItemModel.cidentitycard = "";
                                                payResultResponseItemModel.clocation = "";
                                                payResultResponseItemModel.cpatientcode = "";
                                                payResultResponseItemModel.cpatientname = "";
                                                payResultResponseItemModel.csex = "";
                                                payResultResponseItemModel.ddiagnosetime = "";

                                                payResultResponseItemModel.deptname = "";
                                                payResultResponseItemModel.diagnoseid = "";
                                                payResultResponseItemModel.doctorname = "";
                                                payResultResponseItemModel.ndiagnosenum = "";
                                                payResultResponseItemModel.nmoney = "";
                                            }
                                        }
                                    }
                                    else
                                    {
                                        payResultResponseItemModel.code = "-5";
                                        payResultResponseItemModel.msg = obj_payorder["msg"].ToString();
                                        payResultResponseItemModel.oid = "";
                                        payResultResponseItemModel.cdiagnosetypename = "";
                                        payResultResponseItemModel.cflowcode = "";
                                        payResultResponseItemModel.chousename = "";
                                        payResultResponseItemModel.chousesectionname = "";
                                        payResultResponseItemModel.cidentitycard = "";
                                        payResultResponseItemModel.clocation = "";
                                        payResultResponseItemModel.cpatientcode = "";
                                        payResultResponseItemModel.cpatientname = "";
                                        payResultResponseItemModel.csex = "";
                                        payResultResponseItemModel.ddiagnosetime = "";

                                        payResultResponseItemModel.deptname = "";
                                        payResultResponseItemModel.diagnoseid = "";
                                        payResultResponseItemModel.doctorname = "";
                                        payResultResponseItemModel.ndiagnosenum = "";
                                        payResultResponseItemModel.nmoney = "";
                                    }
                                }
                                return payResultResponseItemModel;
                            }
                            #endregion

                            #region 构造请求参数
                            string appointmentCode = ds.Tables[0].Rows[0]["iid"].ToString();
                            string patientId = ds.Tables[0].Rows[0]["card"].ToString();
                            string patientName = ds.Tables[0].Rows[0]["name"].ToString();
                            string identityCard = ds.Tables[0].Rows[0]["card"].ToString();
                            string birthday = ds.Tables[0].Rows[0]["birth"].ToString();
                            string gender = ds.Tables[0].Rows[0]["gender"].ToString() == "1" ? "男" : "女";
                            DateTime brith = Convert.ToDateTime(ds.Tables[0].Rows[0]["birth"].ToString());
                            string age = ds.Tables[0].Rows[0]["birth"].ToString() == "" ? "1" : CommonBLL.GetAgeByBirthdate(brith).ToString();
                            string telephone = ds.Tables[0].Rows[0]["tel"].ToString();
                            string address = ds.Tables[0].Rows[0]["addr"].ToString();
                            string patientCode = "";// ds.Tables[0].Rows[0]["name"].ToString();
                            string patientCardCode = "";// ds.Tables[0].Rows[0]["name"].ToString();
                            string ageUnit = "岁";//ds.Tables[0].Rows[0]["name"].ToString();
                            string appointmentSource = CommonBLL.GetConfig("source") == "1" ? "趣医网" : "健康山西";
                            #endregion

                            JObject obj_getorder = ChangZhiHePingBLL.MakeOutPatientAppointment(appointmentCode, patientId, patientName, identityCard, birthday, gender, age,
                              telephone, address, patientCode, patientCardCode, ageUnit, appointmentSource);
                            string code_getorder = obj_getorder["code"].ToString();
                            CommonBLL.WriteFile_New(CommonBLL.bizCode_payResult, "3：预约挂号接口code：\r\n" + code_getorder + "   " + string.Equals(code_getorder, "1"));
                            if (string.Equals(code_getorder, "1"))
                            {
                                //保存成功的请求报文
                                if (!bll_notify.Update("1", payResultRequestModel.orderid, obj_getorder["result"].ToString()))
                                {
                                    payResultResponseItemModel.code = "-8";
                                    payResultResponseItemModel.msg = "数据处理出错，请及时排查处理。";
                                    payResultResponseItemModel.oid = "";
                                    payResultResponseItemModel.cdiagnosetypename = "";
                                    payResultResponseItemModel.cflowcode = "";
                                    payResultResponseItemModel.chousename = "";
                                    payResultResponseItemModel.chousesectionname = "";
                                    payResultResponseItemModel.cidentitycard = "";
                                    payResultResponseItemModel.clocation = "";
                                    payResultResponseItemModel.cpatientcode = "";
                                    payResultResponseItemModel.cpatientname = "";
                                    payResultResponseItemModel.csex = "";
                                    payResultResponseItemModel.ddiagnosetime = "";

                                    payResultResponseItemModel.deptname = "";
                                    payResultResponseItemModel.diagnoseid = "";
                                    payResultResponseItemModel.doctorname = "";
                                    payResultResponseItemModel.ndiagnosenum = "";
                                    payResultResponseItemModel.nmoney = "";
                                    return payResultResponseItemModel;
                                }

                                var mJObj_getorder = JArray.Parse(obj_getorder["result"].ToString());
                                foreach (var drGetorder in mJObj_getorder)
                                {
                                    string orderid = drGetorder["AppointmentOrder"]["OrderId"].ToString();

                                    JObject obj_payorder = ChangZhiHePingBLL.GetAppointmentByOrderId(orderid);
                                    string code_payorder = obj_payorder["code"].ToString();
                                    if (string.Equals(code_payorder, "1"))
                                    {
                                        var mJObj_payorder = JArray.Parse(obj_payorder["result"].ToString());
                                        foreach (var drPayorder in mJObj_payorder)
                                        {
                                            if (string.Equals(drPayorder["AppointmentOrder"]["Status"].ToString(), "Available")//0
                                                || string.Equals(drPayorder["AppointmentOrder"]["Status"].ToString(), "Charged")//2
                                                || string.Equals(drPayorder["AppointmentOrder"]["Status"].ToString(), "Triaged"))//3
                                            {
                                                payResultResponseItemModel.code = "1";
                                                payResultResponseItemModel.msg = "支付成功";
                                                payResultResponseItemModel.oid = drPayorder["AppointmentOrder"]["OrderId"].ToString();
                                                payResultResponseItemModel.cdiagnosetypename = drPayorder["WorkSchedule"]["Doctor"]["Rank"].ToString();
                                                payResultResponseItemModel.cflowcode = CommonBLL.GetOrderId(drPayorder["AppointmentOrder"]["OrderId"].ToString(), 4);
                                                payResultResponseItemModel.chousename = drPayorder["WorkSchedule"]["VisitRoom"].ToString();
                                                payResultResponseItemModel.chousesectionname = drPayorder["WorkSchedule"]["VisitRoom"].ToString();
                                                payResultResponseItemModel.cidentitycard = drPayorder["Patient"]["IdentityCard"].ToString();
                                                payResultResponseItemModel.clocation = drPayorder["WorkSchedule"]["VisitRoom"].ToString();
                                                payResultResponseItemModel.cpatientcode = drPayorder["Patient"]["IdentityCard"].ToString();
                                                payResultResponseItemModel.cpatientname = drPayorder["Patient"]["PatientName"].ToString();
                                                payResultResponseItemModel.csex = drPayorder["Patient"]["Gender"].ToString();
                                                payResultResponseItemModel.ddiagnosetime = drPayorder["WorkSchedule"]["Date"].ToString();

                                                payResultResponseItemModel.deptname = drPayorder["WorkSchedule"]["Department"]["Name"].ToString();
                                                payResultResponseItemModel.diagnoseid = drPayorder["AppointmentOrder"]["OrderId"].ToString();
                                                payResultResponseItemModel.doctorname = drPayorder["WorkSchedule"]["Doctor"]["Name"].ToString();
                                                payResultResponseItemModel.ndiagnosenum = drPayorder["WorkDetail"]["AppointmentOrder"].ToString();
                                                payResultResponseItemModel.nmoney = drPayorder["AppointmentOrder"]["Amount"].ToString();
                                            }
                                            else
                                            {
                                                payResultResponseItemModel.code = "1";
                                                payResultResponseItemModel.msg = "支付成功，医院侧订单状态：" + drPayorder["AppointmentOrder"]["Status"].ToString();
                                                payResultResponseItemModel.oid = drPayorder["AppointmentOrder"]["OrderId"].ToString();
                                                payResultResponseItemModel.cdiagnosetypename = "";
                                                payResultResponseItemModel.cflowcode = "";
                                                payResultResponseItemModel.chousename = "";
                                                payResultResponseItemModel.chousesectionname = "";
                                                payResultResponseItemModel.cidentitycard = "";
                                                payResultResponseItemModel.clocation = "";
                                                payResultResponseItemModel.cpatientcode = "";
                                                payResultResponseItemModel.cpatientname = "";
                                                payResultResponseItemModel.csex = "";
                                                payResultResponseItemModel.ddiagnosetime = "";

                                                payResultResponseItemModel.deptname = "";
                                                payResultResponseItemModel.diagnoseid = "";
                                                payResultResponseItemModel.doctorname = "";
                                                payResultResponseItemModel.ndiagnosenum = "";
                                                payResultResponseItemModel.nmoney = "";
                                            }
                                        }
                                    }
                                    else
                                    {
                                        payResultResponseItemModel.code = "-5";
                                        payResultResponseItemModel.msg = obj_payorder["msg"].ToString();
                                        payResultResponseItemModel.oid = "";
                                        payResultResponseItemModel.cdiagnosetypename = "";
                                        payResultResponseItemModel.cflowcode = "";
                                        payResultResponseItemModel.chousename = "";
                                        payResultResponseItemModel.chousesectionname = "";
                                        payResultResponseItemModel.cidentitycard = "";
                                        payResultResponseItemModel.clocation = "";
                                        payResultResponseItemModel.cpatientcode = "";
                                        payResultResponseItemModel.cpatientname = "";
                                        payResultResponseItemModel.csex = "";
                                        payResultResponseItemModel.ddiagnosetime = "";

                                        payResultResponseItemModel.deptname = "";
                                        payResultResponseItemModel.diagnoseid = "";
                                        payResultResponseItemModel.doctorname = "";
                                        payResultResponseItemModel.ndiagnosenum = "";
                                        payResultResponseItemModel.nmoney = "";
                                    }
                                }
                            }
                            else
                            {
                                payResultResponseItemModel.code = "-6";
                                payResultResponseItemModel.msg = obj_getorder["msg"].ToString();
                                payResultResponseItemModel.oid = "";
                                payResultResponseItemModel.cdiagnosetypename = "";
                                payResultResponseItemModel.cflowcode = "";
                                payResultResponseItemModel.chousename = "";
                                payResultResponseItemModel.chousesectionname = "";
                                payResultResponseItemModel.cidentitycard = "";
                                payResultResponseItemModel.clocation = "";
                                payResultResponseItemModel.cpatientcode = "";
                                payResultResponseItemModel.cpatientname = "";
                                payResultResponseItemModel.csex = "";
                                payResultResponseItemModel.ddiagnosetime = "";

                                payResultResponseItemModel.deptname = "";
                                payResultResponseItemModel.diagnoseid = "";
                                payResultResponseItemModel.doctorname = "";
                                payResultResponseItemModel.ndiagnosenum = "";
                                payResultResponseItemModel.nmoney = "";
                            }
                        }
                    }
                }
                catch (Exception err)
                {
                    throw err;
                }

                //返回
                CommonBLL.WriteFile_New(CommonBLL.bizCode_payResult, payResultResponseItemModel.msg);
                return payResultResponseItemModel;
            }
            catch (Exception err)
            {

                throw;
            }
        }
        #endregion

        #region 其他接口

        /// <summary>
        /// 查询就诊状态
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static DiagnoseStateResponseModel DiagnoseState(DiagnoseStateRequestModel diagnoseStateRequestModel)
        {
            try
            {
                DataSet ds = new DataSet();
                //查询就诊状态
                DiagnoseStateResponseModel diagnoseStateResponseModel = new PreeRegisterModel.DiagnoseStateResponseModel();

                //判断入口参数是否有diagnoseid
                if (diagnoseStateRequestModel.diagnoseid.ToString().Trim() == "0")
                {
                    //判断是否同煤
                    if (VariableBLL.IFTmkyy)
                    {
                        bool registerflag = SearchDAL.getregisterflag(diagnoseStateRequestModel);
                        if (registerflag)
                        {
                            ds = SearchDAL.DiagnoseState_tmkyy(diagnoseStateRequestModel);
                        }
                        else
                        {
                            diagnoseStateResponseModel.diagnoseflag = 0;
                            return diagnoseStateResponseModel;
                        }
                    }
                }
                else
                {
                    ds = SearchDAL.DiagnoseState(diagnoseStateRequestModel);
                }



                if (ds == null)
                {
                    return null;
                }

                diagnoseStateResponseModel.diagnoseflag = Common.ToInt(ds.Tables[0].Rows[0][0].ToString());

                //返回
                return diagnoseStateResponseModel;
            }
            catch (Exception err)
            {

                throw;
            }
        }

        /// <summary>
        /// 查询排队叫号
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static ClinicQueueResponseModel ClinicQueue(ClinicQueueRequestModel clinicQueueRequestModel)
        {
            try
            {
                //查询排队叫号
                ClinicQueueResponseModel clinicQueueResponseModel = new PreeRegisterModel.ClinicQueueResponseModel();

                DataSet ds = SearchDAL.ClinicQueue(clinicQueueRequestModel);

                if (ds == null)
                {
                    return null;
                }

                clinicQueueResponseModel.CurrentCallNo = ds.Tables[0].Rows[0]["CurrentCallNo"].ToString();
                clinicQueueResponseModel.DeptCode = ds.Tables[0].Rows[0]["DeptCode"].ToString();
                clinicQueueResponseModel.DeptName = ds.Tables[0].Rows[0]["DeptName"].ToString();
                clinicQueueResponseModel.NotCallNum = ds.Tables[0].Rows[0]["NotCallNum"].ToString();
                clinicQueueResponseModel.staffname = ds.Tables[0].Rows[0]["staffname"].ToString();

                //获取排队叫号
                List<ClinicQueueResponseItemModel> ClinicQueueResponseItemModel_list = new List<ClinicQueueResponseItemModel>();

                foreach (DataRow drClinicQueue in ds.Tables[1].Rows)
                {
                    ClinicQueueResponseItemModel clinicQueueResponseItemModel = new ClinicQueueResponseItemModel();

                    clinicQueueResponseItemModel.cCardCode = drClinicQueue["cCardCode"].ToString();
                    clinicQueueResponseItemModel.cFamilyTel = drClinicQueue["cFamilyTel"].ToString();
                    clinicQueueResponseItemModel.cPatientName = drClinicQueue["cPatientName"].ToString();
                    clinicQueueResponseItemModel.dRegisterDate = drClinicQueue["dRegisterDate"].ToString();
                    clinicQueueResponseItemModel.iRegisterNo = drClinicQueue["iRegisterNo"].ToString();

                    ClinicQueueResponseItemModel_list.Add(clinicQueueResponseItemModel);
                }

                clinicQueueResponseModel.patients = ClinicQueueResponseItemModel_list;

                //返回
                return clinicQueueResponseModel;
            }
            catch (Exception err)
            {

                throw;
            }
        }

        /// <summary>
        /// 查询门诊已缴费
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static ClinicFareResponseModel ClinicFare(ClinicFareRequestModel clinicFareRequestModel)
        {
            try
            {
                //查询门诊已缴费
                ClinicFareResponseModel clinicFareResponseModel = new PreeRegisterModel.ClinicFareResponseModel();

                DataSet ds = SearchDAL.ClinicFare(clinicFareRequestModel);

                //获取排队叫号
                List<ClinicFareResponseItemModel> ClinicFareResponseItemModel_list = new List<ClinicFareResponseItemModel>();

                foreach (DataRow drClinicFare in ds.Tables[0].Rows)
                {
                    ClinicFareResponseItemModel clinicFareResponseItemModel = new ClinicFareResponseItemModel();

                    clinicFareResponseItemModel.ccheckcode = drClinicFare["ccheckcode"].ToString();
                    clinicFareResponseItemModel.cItemCode = drClinicFare["cItemCode"].ToString();
                    clinicFareResponseItemModel.cItemName = drClinicFare["cItemName"].ToString();
                    clinicFareResponseItemModel.cRateType = drClinicFare["cRateType"].ToString();
                    clinicFareResponseItemModel.cStandard = drClinicFare["cStandard"].ToString();
                    clinicFareResponseItemModel.faredate = drClinicFare["DT"].ToString();
                    clinicFareResponseItemModel.nMoney = drClinicFare["nMoney"].ToString();
                    clinicFareResponseItemModel.nNum = drClinicFare["nNum"].ToString();
                    clinicFareResponseItemModel.nPrice = drClinicFare["nPrice"].ToString();
                    clinicFareResponseItemModel.totalMoney = drClinicFare["totalMoney"].ToString();
                    clinicFareResponseItemModel.idetailid = drClinicFare["iFlowID"].ToString();
                    clinicFareResponseItemModel.cInterPayFromType = drClinicFare["cInterPayFromType"].ToString();

                    ClinicFareResponseItemModel_list.Add(clinicFareResponseItemModel);
                }

                clinicFareResponseModel.items = ClinicFareResponseItemModel_list;

                //返回
                return clinicFareResponseModel;
            }
            catch (Exception err)
            {

                throw;
            }
        }

        /// <summary>
        /// 住院费用查询
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static HospitalFareResponseModel HospitalFare(HospitalFareRequestModel HospitalFareRequestModel)
        {
            try
            {
                //住院费用查询
                HospitalFareResponseModel hospitalFareResponseModel = new PreeRegisterModel.HospitalFareResponseModel();

                DataSet ds = SearchDAL.HospitalFare(HospitalFareRequestModel);

                if (ds == null)
                {
                    return null;
                }

                //赋值
                hospitalFareResponseModel.AdmissionDate = ds.Tables[0].Rows[0]["AdmissionDate"].ToString();
                hospitalFareResponseModel.BedCode = ds.Tables[0].Rows[0]["BedCode"].ToString();
                hospitalFareResponseModel.InPCode = ds.Tables[0].Rows[0]["InPCode"].ToString();
                hospitalFareResponseModel.leftmoney = ds.Tables[0].Rows[0]["ye"].ToString();
                hospitalFareResponseModel.No_TreatList = ds.Tables[0].Rows[0]["No_TreatList"].ToString();
                hospitalFareResponseModel.PatName = ds.Tables[0].Rows[0]["PatName"].ToString();
                hospitalFareResponseModel.PayMoney = ds.Tables[0].Rows[0]["PayMoney"].ToString();
                hospitalFareResponseModel.RoomName = ds.Tables[0].Rows[0]["RoomName"].ToString();
                hospitalFareResponseModel.totalfare = ds.Tables[0].Rows[0]["totalfare"].ToString();
                hospitalFareResponseModel.MarkOutdate = ds.Tables[0].Rows[0]["MarkOutdate"].ToString();
                hospitalFareResponseModel.DeptName = ds.Tables[0].Rows[0]["DeptName"].ToString();

                List<HospitalFareResponseItemModel> HospitalFareResponseItemModel_list = new List<HospitalFareResponseItemModel>();

                foreach (DataRow drHospitalFare in ds.Tables[1].Rows)
                {
                    HospitalFareResponseItemModel hospitalFareResponseItemModel = new HospitalFareResponseItemModel();

                    hospitalFareResponseItemModel.FareClassName = drHospitalFare["FareClassName"].ToString();
                    hospitalFareResponseItemModel.FareItemName = drHospitalFare["FareItemName"].ToString();
                    hospitalFareResponseItemModel.FareItemSpec = drHospitalFare["FareItemSpec"].ToString();
                    hospitalFareResponseItemModel.FareItemUnit = drHospitalFare["FareItemUnit"].ToString();
                    hospitalFareResponseItemModel.InFactMoney = drHospitalFare["InFactMoney"].ToString();
                    hospitalFareResponseItemModel.InMoney = drHospitalFare["InMoney"].ToString();
                    hospitalFareResponseItemModel.Quantity = drHospitalFare["Quantity"].ToString();
                    hospitalFareResponseItemModel.InTime = drHospitalFare["InTime"].ToString();

                    HospitalFareResponseItemModel_list.Add(hospitalFareResponseItemModel);
                }

                hospitalFareResponseModel.items = HospitalFareResponseItemModel_list;

                //返回
                return hospitalFareResponseModel;
            }
            catch (Exception err)
            {

                throw;
            }
        }

        /// <summary>
        /// 人工窗口退费查询
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static WindowFadeFareResponseModel WindowFadeFare(WindowFadeFareRequestModel windowFadeFareRequestModel)
        {
            try
            {
                //人工窗口退费查询
                WindowFadeFareResponseModel windowFadeFareResponseModel = new PreeRegisterModel.WindowFadeFareResponseModel();

                DataSet ds = SearchDAL.WindowFadeFare(windowFadeFareRequestModel);

                if (ds == null || ds.Tables[0].Rows.Count < 1)
                {
                    windowFadeFareResponseModel.code = "0";
                    windowFadeFareResponseModel.msg = "未查询到记录";
                    return windowFadeFareResponseModel;
                }

                //赋值
                windowFadeFareResponseModel.code = "1";
                windowFadeFareResponseModel.msg = "查询记录成功！";

                List<WindowFadeFareResponseResultModel> windowFadeFareResponseResultModel_list = new List<WindowFadeFareResponseResultModel>();

                foreach (DataRow drWindowFadeFare in ds.Tables[0].Rows)
                {
                    WindowFadeFareResponseResultModel windowFadeFareResponseResultModel = new WindowFadeFareResponseResultModel();
                    windowFadeFareResponseResultModel.cFlowCode = ds.Tables[0].Rows[0]["cFlowCode"].ToString();
                    windowFadeFareResponseResultModel.cPatientCode = ds.Tables[0].Rows[0]["cPatientCode"].ToString();
                    windowFadeFareResponseResultModel.cPatientName = ds.Tables[0].Rows[0]["cPatientName"].ToString();
                    windowFadeFareResponseResultModel.cRateType = ds.Tables[0].Rows[0]["cRateType"].ToString();
                    windowFadeFareResponseResultModel.nMoney = ds.Tables[0].Rows[0]["nMoney"].ToString();

                    windowFadeFareResponseResultModel_list.Add(windowFadeFareResponseResultModel);
                }

                windowFadeFareResponseModel.result = windowFadeFareResponseResultModel_list;

                //返回
                return windowFadeFareResponseModel;
            }
            catch (Exception err)
            {

                throw;
            }
        }

        /// <summary>
        /// 门诊费用发票号查询
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static ClinicFareBillNumResponseModel ClinicFareBillNum(ClinicFareBillNumRequestModel clinicFareBillNumRequestModel)
        {
            try
            {
                //门诊费用发票号查询
                ClinicFareBillNumResponseModel clinicFareBillNumResponseModel = new PreeRegisterModel.ClinicFareBillNumResponseModel();

                DataSet ds = SearchDAL.ClinicFareBillNum(clinicFareBillNumRequestModel);

                if (ds == null || ds.Tables[0].Rows.Count < 1)
                {
                    clinicFareBillNumResponseModel.code = "0";
                    clinicFareBillNumResponseModel.msg = "未查询到记录";
                    return clinicFareBillNumResponseModel;
                }

                //判断是否打印发票号 liujia151124
                if (ds.Tables[0].Rows[0]["cBillCode"].ToString().Trim() == "" || ds.Tables[0].Rows[0]["cBillCode"].ToString().Trim() == "无")
                {
                    clinicFareBillNumResponseModel.code = "0";
                    clinicFareBillNumResponseModel.msg = "未查询到打印的发票号！";
                    clinicFareBillNumResponseModel.billcode = ds.Tables[0].Rows[0]["cBillCode"].ToString();
                }
                else
                {
                    clinicFareBillNumResponseModel.code = "1";
                    clinicFareBillNumResponseModel.msg = "查询到打印的发票号！";
                    clinicFareBillNumResponseModel.billcode = ds.Tables[0].Rows[0]["cBillCode"].ToString();
                }

                //返回
                return clinicFareBillNumResponseModel;
            }
            catch (Exception err)
            {

                throw;
            }
        }

        /// <summary>
        /// 置人工窗口退费流水已获取标志  liujia151208
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static SetWFFFlagResponseModel SetWFFFlag(SetWFFFlagRequestModel setWFFFlagRequestModel)
        {
            try
            {
                //置人工窗口退费流水已获取标志
                SetWFFFlagResponseModel setWFFFlagResponseModel = new PreeRegisterModel.SetWFFFlagResponseModel();

                if (SearchDAL.SetWFFFlag(setWFFFlagRequestModel))
                {
                    setWFFFlagResponseModel.code = "1";
                    setWFFFlagResponseModel.msg = "回置人工窗口退费流水标志成功！";
                }
                else
                {
                    setWFFFlagResponseModel.code = "0";
                    setWFFFlagResponseModel.msg = "回置人工窗口退费流水标志失败！";
                }

                //返回
                return setWFFFlagResponseModel;
            }
            catch (Exception err)
            {

                throw;
            }
        }

        /// <summary>
        /// 查询挂号记录  liujia151226
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static RegisterRecordResponseModel RegisterRecord(RegisterRecordRequestModel registerRecordRequestModel)
        {
            try
            {
                DataSet dsRegister = new DataSet();

                RegisterRecordResponseModel registerRecordResponseModel = new PreeRegisterModel.RegisterRecordResponseModel();

                //根据诊疗ID号与排班信息查询挂号记录
                if (registerRecordRequestModel.orderid.Trim() == "")
                {
                    string wid = WorkGetDAL.GetWorkPlanID(registerRecordRequestModel.doctorid.Trim(), registerRecordRequestModel.date.Trim(), registerRecordRequestModel.time.Trim());

                    dsRegister = SearchDAL.GetRegisterRecord_ByPatientCodeAndWid(registerRecordRequestModel, wid);
                }
                else  //根据预约订单号查询挂号记录
                {
                    dsRegister = SearchDAL.GetRegisterRecord_ByOrderid(registerRecordRequestModel);
                }

                if (dsRegister == null || dsRegister.Tables[0].Rows.Count < 1)
                {
                    registerRecordResponseModel.code = "0";
                    registerRecordResponseModel.msg = "查询挂号记录失败：未查询到挂号记录！";
                    return registerRecordResponseModel;
                }

                //查询就诊记录
                DataSet dsDiagnose = new DataSet();

                DiagnoseStateRequestModel diagnoseStateRequestModel = new DiagnoseStateRequestModel();
                diagnoseStateRequestModel.diagnoseid = Common.ToInt(dsRegister.Tables[0].Rows[0]["iDiagnoseCode"]);
                dsDiagnose = SearchDAL.DiagnoseState(diagnoseStateRequestModel);
                if (dsDiagnose == null)
                {
                    registerRecordResponseModel.code = "0";
                    registerRecordResponseModel.msg = "查询挂号记录失败：未查询到就诊记录！";
                    return registerRecordResponseModel;
                }

                //赋值挂号与就诊数据
                registerRecordResponseModel.code = "1";
                registerRecordResponseModel.msg = "查询挂号记录成功！";
                registerRecordResponseModel.deptname = dsRegister.Tables[0].Rows[0]["DeptName"].ToString();
                registerRecordResponseModel.doctorname = dsRegister.Tables[0].Rows[0]["StaffName"].ToString();
                registerRecordResponseModel.ddiagnosetime = Common.ToDateTime(dsRegister.Tables[0].Rows[0]["dstarttime"]).ToShortDateString();
                registerRecordResponseModel.time = dsRegister.Tables[0].Rows[0]["cTimeTypeName"].ToString();
                registerRecordResponseModel.ndiagnosenum = dsRegister.Tables[0].Rows[0]["nDiagnoseNum"].ToString();
                registerRecordResponseModel.cflowcode = dsRegister.Tables[0].Rows[0]["cFlowCode"].ToString();
                registerRecordResponseModel.diagnoseid = dsRegister.Tables[0].Rows[0]["iDiagnoseCode"].ToString();
                registerRecordResponseModel.diagnoseflag = dsDiagnose.Tables[0].Rows[0]["diagnoseflag"].ToString();
                registerRecordResponseModel.cpatientcode = dsRegister.Tables[0].Rows[0]["cPatientCode"].ToString();
                registerRecordResponseModel.cpatientname = dsRegister.Tables[0].Rows[0]["cPatientName"].ToString();

                //返回
                return registerRecordResponseModel;
            }
            catch (Exception err)
            {

                throw;
            }
        }

        #endregion
    }
}
